package com.ruoyi.xiaoshou.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.activiti.api.RemoteActivitiService;
import com.ruoyi.basedata.api.RemoteGoodsService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.dto.ActApprovalDTO;
import com.ruoyi.xiaoshou.domain.SaleRequestDetail;
import com.ruoyi.xiaoshou.dto.AddOrderDto;
import com.ruoyi.xiaoshou.mapper.SaleRequestDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xiaoshou.mapper.SaleRequestMapper;
import com.ruoyi.xiaoshou.domain.SaleRequest;
import com.ruoyi.xiaoshou.service.ISaleRequestService;

/**
 * 销售申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-26
 */
@Service
public class SaleRequestServiceImpl implements ISaleRequestService 
{
    @Autowired
    private SaleRequestMapper saleRequestMapper;

    @Autowired
    private SaleRequestDetailMapper saleRequestDetailMapper;

    @Autowired
    private RemoteActivitiService remoteActivitiService;

    @Autowired
    private RemoteGoodsService remoteGoodsService;

    @Override
    public SaleRequest getReqByCode(String code) {
        return saleRequestMapper.getReqByCode(code);
    }

    @Override
    public int updSaleReq(SaleRequest saleRequest) {
        return saleRequestMapper.updSaleReq(saleRequest);
    }

    @Override
    public AjaxResult getAllRequest() {
        List<SaleRequest> saleRequestList = saleRequestMapper.getAllRequest();
        return AjaxResult.success(saleRequestList);
    }

    @Override
    public AjaxResult addOrder(AddOrderDto addOrderDto) {
        //添加销售申请订单
        BigDecimal totalCost = BigDecimal.ZERO;
        String code = "XSSQDD"+System.currentTimeMillis();
        SaleRequest saleRequest = new SaleRequest();
        saleRequest.setCode(code);
        saleRequest.setCreateBy(SecurityUtils.getUsername());
        saleRequest.setCusName(addOrderDto.getCusName());
        saleRequest.setCusId(addOrderDto.getCusId());
        for (SaleRequestDetail requestDetail:addOrderDto.getOrderDetail()){
            BigDecimal cost = remoteGoodsService.getCostByGoodsId(requestDetail.getProId()).getData();
            requestDetail.setCostPrice(cost);
            BigDecimal result = cost.multiply(BigDecimal.valueOf(requestDetail.getCount()));
            totalCost  = totalCost.add(result);
        }
        saleRequest.setCostPrice(totalCost);
        int count= saleRequestMapper.addOrder(saleRequest);
        //获取新添加的订单id
        int orderId = saleRequestMapper.getOrderId(code);
        //添加订单详情
        Boolean isPrice = true;
        for (SaleRequestDetail requestDetail:addOrderDto.getOrderDetail()){
            requestDetail.setOrderId(orderId);
            int count1 = saleRequestDetailMapper.addOrderDetail(requestDetail);
            int  price = saleRequestDetailMapper.getPrice(requestDetail.getProId());
            if(requestDetail.getPrice().compareTo(BigDecimal.valueOf(price))<0){
                //有商品价格低于低价
                isPrice = false;
            }
        }
        saleRequest.setModifyBy(SecurityUtils.getUsername());
        saleRequest.setId(orderId);
        ActApprovalDTO actApprovalDTO = new ActApprovalDTO();
        actApprovalDTO.setProcessDefinitionKey("saleReqBPMN");
        actApprovalDTO.setOrderCode(code);
        actApprovalDTO.setTaskName(code+"销售申请");
        actApprovalDTO.setDeptId(103);
        AjaxResult ajaxResult = remoteActivitiService.addApproval(actApprovalDTO);
        Integer codeNum = (Integer) ajaxResult.get("code");
        if(codeNum != 200){
            throw new ServiceException("系统错误，添加失败");
        }
        if(isPrice){
            saleRequest.setAuditName("系统");
            saleRequest.setStatus(1);
            //activiti系统自动审核
            String processInstanceID = (String) ajaxResult.get("msg");
//            remoteActivitiService.saleHandelApproval(processInstanceID);
            //通过系统底价审核，自动审核通过
        }else{
            saleRequest.setAuditName("");
            saleRequest.setStatus(0);
        }
        int count2 = saleRequestMapper.updOrder(saleRequest);
        return AjaxResult.success("添加成功");
    }

    /**
     * 查询销售申请
     * 
     * @param id 销售申请主键
     * @return 销售申请
     */
    @Override
    public SaleRequest selectSaleRequestById(Integer id)
    {
        return saleRequestMapper.selectSaleRequestById(id);
    }

    /**
     * 查询销售申请列表
     * 
     * @param saleRequest 销售申请
     * @return 销售申请
     */
    @Override
    public List<SaleRequest> selectSaleRequestList(SaleRequest saleRequest)
    {
        return saleRequestMapper.selectSaleRequestList(saleRequest);
    }

    /**
     * 新增销售申请
     * 
     * @param saleRequest 销售申请
     * @return 结果
     */
    @Override
    public int insertSaleRequest(SaleRequest saleRequest)
    {
        return saleRequestMapper.insertSaleRequest(saleRequest);
    }

    /**
     * 修改销售申请
     * 
     * @param saleRequest 销售申请
     * @return 结果
     */
    @Override
    public int updateSaleRequest(SaleRequest saleRequest)
    {
        return saleRequestMapper.updateSaleRequest(saleRequest);
    }

    /**
     * 批量删除销售申请
     * 
     * @param ids 需要删除的销售申请主键
     * @return 结果
     */
    @Override
    public int deleteSaleRequestByIds(Integer[] ids)
    {
        return saleRequestMapper.deleteSaleRequestByIds(ids);
    }

    /**
     * 删除销售申请信息
     * 
     * @param id 销售申请主键
     * @return 结果
     */
    @Override
    public int deleteSaleRequestById(Integer id)
    {
        return saleRequestMapper.deleteSaleRequestById(id);
    }
}
